﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using XBS.Model;
using XBS.ViewModel;
using System.Windows.Media;
using XBS.Converter;
using System.IO.IsolatedStorage;

namespace XBS
{
    public partial class SettingPage : PhoneApplicationPage
    {
        public SettingPage()
        {
            InitializeComponent();

            DataContext = App.ViewModel;

            SliderFontSize.ValueChanged += SliderFontSize_ValueChanged;
            ListTextColor.ListColors.SelectionChanged += ListTextColors_SelectionChanged;
            ListBackgroundColor.ListColors.SelectionChanged += ListBackgroundColors_SelectionChanged;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        private void SliderFontSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(VM.KEY_FONT_SIZE))
            {
                IsolatedStorageSettings.ApplicationSettings[VM.KEY_FONT_SIZE] = App.ViewModel.FontSize;
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings.Add(VM.KEY_FONT_SIZE, App.ViewModel.FontSize);
            }
        }

        void ListTextColors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                CustomColor selectedColor = (CustomColor)e.AddedItems[0];
                Color color = selectedColor.Value;
                App.ViewModel.updateListTextColors(color);
            }

            App.ViewModel.SaveTextColorSetting();
        }

        void ListBackgroundColors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                CustomColor selectedColor = (CustomColor)e.AddedItems[0];
                Color color = selectedColor.Value;
                App.ViewModel.updateListBackgroundColor(color);
            }

            App.ViewModel.SaveBackgroundColorSetting();
        }
    }
}